package com.rui1024.servlet;

import com.rui1024.dao.DiaryDao;
import com.rui1024.dao.impl.DiaryDaoImpl;
import com.rui1024.pojo.Diary;
import com.rui1024.util.JDBCUtil;
import com.rui1024.util.StringUtil;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;


public class DiaryServlet extends HttpServlet {

    DiaryDao diaryDao = new DiaryDaoImpl();
    JDBCUtil jdbcUtil = new JDBCUtil();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");

        String action = req.getParameter("action");
        if("show".equals(action)){
            this.diaryShow(req,resp);
        }else if("preSave".equals(action)){
            this.diaryPreSave(req,resp);
        }else if("save".equals(action)){
            this.diarySave(req,resp);
        }else if("delete".equals(action)){
            this.diaryDelete(req,resp);
        }
    }

    public void diaryShow(HttpServletRequest req, HttpServletResponse resp){
        String diaryId = req.getParameter("diaryId");
        Connection con=null;
        try {
            con = jdbcUtil.getCon();
            Diary diary = diaryDao.diaryShow(con, Integer.parseInt(diaryId));
            req.setAttribute("diary",diary);
            req.setAttribute("mainPage","diary/diaryShow.jsp");
            req.getRequestDispatcher("mainTemp.jsp").forward(req,resp);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                jdbcUtil.closeCon(con);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 打开添加页面
     * @param req
     * @param resp
     */
    public void diaryAdd(HttpServletRequest req, HttpServletResponse resp){
        try {
            req.setAttribute("mainPage","diary/diaryAdd.jsp");
            req.getRequestDispatcher("mainTemp.jsp").forward(req,resp);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 保存：根据是否有diaryId来判断进行添加还是修改操作
     * @param req
     * @param resp
     */
    public void diarySave(HttpServletRequest req, HttpServletResponse resp){
        String title=req.getParameter("title");
        String content=req.getParameter("content");
        String typeId=req.getParameter("typeId");
        String diaryId=req.getParameter("diaryId");

        Diary diary = new Diary(title, content, Integer.parseInt(typeId));
        if(StringUtil.isNotEmpty(diaryId)){
            diary.setDiaryId(Integer.parseInt(diaryId));
        }
        Connection con=null;
        try {
            con = jdbcUtil.getCon();
            int i=0;
            if(StringUtil.isNotEmpty(diaryId)){
                i = diaryDao.diaryUpdate(con, diary);
            }else{
                i = diaryDao.diaryAdd(con, diary);
            }
            if(i>0){
                req.getRequestDispatcher("main").forward(req,resp);
            }else{
                req.setAttribute("diary",diary);
                req.setAttribute("error","保存失败");
                req.setAttribute("mainPage","diary/diaryAdd.jsp");
                req.getRequestDispatcher("mainTemp.jsp").forward(req,resp);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                jdbcUtil.closeCon(con);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public void diaryDelete(HttpServletRequest req, HttpServletResponse resp){
        String diaryId=req.getParameter("diaryId");
        Connection con=null;
        try {
            con = jdbcUtil.getCon();
            int i = diaryDao.diaryDelete(con,Integer.parseInt(diaryId));
            req.getRequestDispatcher("main").forward(req,resp);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                jdbcUtil.closeCon(con);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 修改日志用到：根据需要修改的日志id查出该日志的详细信息，加载到页面中
     * @param req
     * @param resp
     */
    public void diaryPreSave(HttpServletRequest req, HttpServletResponse resp){
        String diaryId=req.getParameter("diaryId");
        Connection con=null;
        try {
            con = jdbcUtil.getCon();
            if(StringUtil.isEmpty(diaryId)){
                req.setAttribute("mainPage","diary/diaryPreSave.jsp");
                req.getRequestDispatcher("mainTemp.jsp").forward(req,resp);
            }else{
                Diary diary = diaryDao.diaryShow(con, Integer.parseInt(diaryId));
                req.setAttribute("diary",diary);
                req.setAttribute("mainPage","diary/diaryPreSave.jsp");
                req.getRequestDispatcher("mainTemp.jsp").forward(req,resp);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                jdbcUtil.closeCon(con);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
